package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Strategy;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.domain.DestinationTemplate;
import cn.wolfcode.luowowo.search.domain.StrategyTemplate;
import cn.wolfcode.luowowo.search.domain.TravelTemplate;
import cn.wolfcode.luowowo.search.domain.UserInfoTemplate;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import cn.wolfcode.luowowo.website.web.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class IndexController {

    @Reference
    private IUserInfoRedisService userInfoRedisService;
    @Reference
    private ITravelCommendService travelCommendService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private ITravelService travelService;
    @Reference
    private ISearchService searchService;
    @Reference
    private IStrategySearchService strategySearchService;
    @Reference
    private IDestinationSearchService destinationSearchService;
    @Reference
    private ITravelSearchService travelSearchService;
    @Reference
    private IUserInfoSearchService userInfoSearchService;



    @RequestMapping("")
    public String index(Model model){

        //tcs   游记推荐前五
        model.addAttribute("tcs",travelCommendService.getTop5());

        //sc    攻略推荐第一个
        model.addAttribute("sc",strategyCommendService.getTop5().get(0));

        return "index/index";
    }


    @RequestMapping("/index/travelPage")
    public String travelPage(Model model, TravelQuery qo){

        //pageInfo  全部游记分页
        model.addAttribute("pageInfo",travelService.query(qo));

        return "index/travelPageTpl";
    }

    @RequestMapping("/q")
    public String search(Model model,@ModelAttribute("qo") SearchQueryObject qo){
        switch (qo.getType()) {
            case SearchQueryObject.SEARCH_TYPE_DEST:
                return searchDest(model, qo);
            case SearchQueryObject.SEARCH_TYPE_STRATEGY:
                return searchStrategy(model, qo);
            case SearchQueryObject.SEARCH_TYPE_TRAVEL:
                return searchTravel(model, qo);
            case SearchQueryObject.SEARCH_TYPE_USER:
                return searchUser(model, qo);
            default:
                return searchAll(model, qo);
        }
    }
    private String searchAll(Model model, SearchQueryObject qo) {
        SearchResultVO result = new SearchResultVO();
        Page<UserInfoTemplate> su = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class,
                qo, "nickname", "destName");
        Page<TravelTemplate> st = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class,
                qo, "title", "summary");
        Page<StrategyTemplate> ss = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class,
                qo, "title", "summary","subTitle");
        Page<DestinationTemplate> sd = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME, DestinationTemplate.class,
                qo, "name", "info");
        long totle = su.getTotalElements() + sd.getTotalElements() + ss.getTotalElements() + st.getTotalElements();
        result.setDests(sd.getContent());
        result.setStrategys(ss.getContent());
        result.setTravels(st.getContent());
        result.setUsers(su.getContent());
        result.setTotal(totle);
        model.addAttribute("data",result);
        return "index/searchAll";
    }

    private String searchUser(Model model, SearchQueryObject qo) {
        Page<UserInfoTemplate> su = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class,
                qo, "nickname", "destName");
        model.addAttribute("page",su);
        return "index/searchUser";
    }

    private String searchTravel(Model model, SearchQueryObject qo) {
        Page<TravelTemplate> st = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class,
                qo, "title", "summary");
        model.addAttribute("page",st);
        return "index/searchTravel";
    }

    private String searchStrategy(Model model, SearchQueryObject qo) {
        Page<StrategyTemplate> ss = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class,
                qo, "title", "summary","subTitle");
        model.addAttribute("page",ss);
        return "index/searchStrategy";
    }

    private String searchDest(Model model, SearchQueryObject qo) {
        //判断当前关键字是不是目的地
        DestinationTemplate dest = destinationSearchService.findByName(qo.getKeyword());

        //如果是,查询该目的地下的攻略,游记,用户
        if (dest != null){
            SearchResultVO result = new SearchResultVO();
            List<TravelTemplate> ts = travelSearchService.findByDestName(dest.getName());
            result.setTotal(result.getTotal() + ts.size());
            if (ts.size()>5){
                ts = ts.subList(0,5);
            }

            List<UserInfoTemplate> us = userInfoSearchService.findByDestName(dest.getName());
            result.setTotal(result.getTotal() + us.size());
            if (us.size()>5){
                us = us.subList(0,5);
            }
            List<StrategyTemplate> ss = strategySearchService.findByDestName(dest.getName());
            result.setTotal(result.getTotal() + ss.size());
            if (ss.size()>5){
                ss = ss.subList(0,5);
            }
            result.setUsers(us);
            result.setTravels(ts);
            result.setStrategys(ss);
            model.addAttribute("data",result);
        }
        //如果不是,提示
        model.addAttribute("dest",dest);

        return "index/searchDest";
    }

}
